package NewTmas.EditMode;

import NewTmas.Graphs.GraphVizGraph;
import taskmaster.util.LookupLists;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 8/18/11
 * Time: 6:27 PM
 * To change this template use File | Settings | File Templates.
 */
public class CopyJobListener implements ActionListener
{

    String jobname;
    GraphVizGraph graphVizGraph;
    Connection connection;
    EditIntFrame editIntFrame;
    LookupLists ll;

    public CopyJobListener(String jobname, GraphVizGraph graphVizGraph, Connection connection, EditIntFrame editIntFrame, LookupLists ll)
    {
        this.jobname = jobname;
        this.graphVizGraph = graphVizGraph;
        this.connection = connection;
        this.editIntFrame = editIntFrame;
        this.ll = ll;
    }


    public void actionPerformed(ActionEvent e)
    {
        String[] userJobs = null;
        try
        {
            userJobs = ll.getUsersJobs();
        } catch (Exception e1)
        {
            JOptionPane.showConfirmDialog(null, "Error getting user jobs : " + e1.getMessage(), "Error", JOptionPane.DEFAULT_OPTION);
            e1.printStackTrace();
        }

        Arrays.sort(userJobs);
        JComboBox sourceJobCbox = new JComboBox(userJobs);
        sourceJobCbox.setSelectedItem(jobname);
        JTextField newjobField = new JTextField();
        Object[] message = new Object[]{"Source Job", sourceJobCbox, "Dest Job ", newjobField};
        int input = JOptionPane.showConfirmDialog(null, message, "Select Job to copy and type name for destination", JOptionPane.OK_CANCEL_OPTION);
        if (input == JOptionPane.OK_OPTION)
        {
            String sourceJobName = sourceJobCbox.getSelectedItem().toString();
            String destinationJobName = newjobField.getText();

            CallStoredProc storedProc = new CallStoredProc("{call tms.job_util.copy(?,?)}", new String[]{sourceJobName, destinationJobName}, connection);

            editIntFrame.sqlCommands.add(storedProc);
            editIntFrame.model.addElement(storedProc);
            editIntFrame.sqlcommandsList.setModel(editIntFrame.model);

        }

    }
}
